package com.platform.core.security.auth;

import com.platform.comm.util.CommonWriter;
import com.platform.comm.web.results.JsonResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description: 自定义登录失败
 *
 * @author sunzhubin.zh
 */
@Component
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception) throws IOException {
        //用户登录时身份认证未通过
        CommonWriter.write(JsonResult.of(exception), response);
    }
}
